<template>
  <a-layout style="padding: 24px 0; background: #fff">
    <a-layout-content :style="{ padding: '24px', minHeight: '280px' }">
      <h3 v-if="level1.length == 0"> 对不起，找不到相关文档 </h3>
      <a-row>
        <a-col :span="4">
          <a-tree
              v-if="level1.length > 0"
              :defaultExpandAll="true"
              :tree-data="level1"
              :fieldNames="{children:'children', title:'name', key:'id' }"
              :selectedKeys="selectKeys"
              @select="onSelect"
          />
        </a-col>
        <a-col :span="20">
            <div>
              <h2>{{ doc.name }}</h2>
              <div>
                <span>阅读数：{{doc.viewCount}}</span>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <span>点赞数：{{doc.voteCount}}</span>
              </div>
              <a-divider />
            </div>
            <div v-html="html" class="editor-content-view"></div>

            <div>
              <a-button type="primary" shape="round" @click="vote">
                <template #icon><like-outlined /></template>
                点赞 {{doc.voteCount}}
              </a-button>
            </div>
        </a-col>
      </a-row>
    </a-layout-content>
  </a-layout>
</template>

<script>
import {ref} from "vue";
import axios from "axios";
import {message} from "ant-design-vue";
import {Tool} from "@/util/tool.ts";
import {useRoute} from "vue-router";
import { LikeOutlined } from '@ant-design/icons-vue';


export default {
  name: "doc",
  components:{
    LikeOutlined
  },
  setup(){
    const level1 = ref({});
    const docs = ref();
    const route = useRoute();
    const ebookId = route.query.ebookId;
    const selectKeys = ref([]);
    const html = ref();

    const doc = ref({});
    // 查询所有数据
    const handleQuery = (params) => {
      params.ebookId = ebookId;
      axios.get("/doc/list", {params}).then((response) => {
        const data = response.data;
        if (data.code == 200){
          docs.value = data.data;
          level1.value = Tool.array2Tree(docs.value, 0);
          selectKeys.value.push(docs.value[0].id);
          doc.value = docs.value[0];
          getContent(docs.value[0].id)
        } else {
          message.error(data.message)
        }
      });
    };



    const getContent = (key) => {
      axios.get("/content/get/" + key).then((response) => {
        const data = response.data;
        if (data.code == 200){
          html.value = data.data.content;
        } else {
          message.error(data.message)
        }
      });
    }
    
    const onSelect = (keys, infos) => {
      doc.value = infos.node;
      selectKeys.value = keys;
      getContent(keys[0]);
    }

    // 点赞
    const vote = ()=>{
      axios.get("/doc/vote/" + doc.value.id).then((response) => {
        const data = response.data;
        if (data.code == 200){
          doc.value.voteCount ++;
        } else {
          message.error(data.message)
        }
      });
    }
    return {
      level1,
      handleQuery,
      html,
      selectKeys,
      onSelect,
      doc,
      vote
    }
  },
  mounted() {
    this.handleQuery({});
  }
}
</script>

<style>
.editor-content-view {
  border-radius: 5px;
  padding: 0 10px;
  margin-top: 20px;
  overflow-x: auto;
}

.editor-content-view p,
.editor-content-view li {
  white-space: pre-wrap; /* 保留空格 */
}

.editor-content-view blockquote {
  border-left: 8px solid #d0e5f2;
  padding: 10px 10px;
  margin: 10px 0;
  background-color: #f1f1f1;
}

.editor-content-view code {
  font-family: monospace;
  background-color: #eee;
  padding: 3px;
  border-radius: 3px;
}
.editor-content-view pre>code {
  display: block;
  padding: 10px;
}

.editor-content-view table {
  border-collapse: collapse;
}
.editor-content-view td,
.editor-content-view th {
  border: 1px solid #ccc;
  min-width: 50px;
  height: 20px;
}
.editor-content-view th {
  background-color: #f1f1f1;
}

.editor-content-view ul,
.editor-content-view ol {
  padding-left: 20px;
}

.editor-content-view input[type="checkbox"] {
  margin-right: 5px;
}
</style>